package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.controller.es.EsUnService;
import com.ruoyi.system.controller.es.dto.EsSearch;
import com.ruoyi.system.domain.vo.web.WebFyPolicyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FyPolicyMapper;
import com.ruoyi.system.domain.FyPolicy;
import com.ruoyi.system.service.IFyPolicyService;

/**
 * 政策Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@Service
public class FyPolicyServiceImpl implements IFyPolicyService
{
    @Autowired
    private FyPolicyMapper fyPolicyMapper;
    @Autowired
    private EsUnService esUnService;
    /**
     * 查询政策
     *
     * @param policyId 政策主键
     * @return 政策
     */
    @Override
    public FyPolicy selectFyPolicyByPolicyId(Long policyId)
    {
        return fyPolicyMapper.selectFyPolicyByPolicyId(policyId);
    }

    /**
     * 查询政策列表
     *
     * @param fyPolicy 政策
     * @return 政策
     */
    @Override
    public List<FyPolicy> selectFyPolicyList(FyPolicy fyPolicy)
    {
        return fyPolicyMapper.selectFyPolicyList(fyPolicy);
    }

    @Override
    public List<WebFyPolicyVo> selectWebPolicyList(String policyTitle) {
        return fyPolicyMapper.selectWebPolicyList(policyTitle);
    }

    /**
     * 新增政策
     *
     * @param fyPolicy 政策
     * @return 结果
     */
    @Override
    public int insertFyPolicy(FyPolicy fyPolicy)
    {
        fyPolicy.setIsDelete(0l);
        fyPolicy.setCreateTime(DateUtils.getNowDate());
        return fyPolicyMapper.insertFyPolicy(fyPolicy);
    }

    /**
     * 修改政策
     *
     * @param fyPolicy 政策
     * @return 结果
     */
    @Override
    public int updateFyPolicy(FyPolicy fyPolicy)
    {
        fyPolicy.setUpdateTime(DateUtils.getNowDate());
        return fyPolicyMapper.updateFyPolicy(fyPolicy);
    }

    /**
     * 批量删除政策
     *
     * @param policyIds 需要删除的政策主键
     * @return 结果
     */
    @Override
    public int deleteFyPolicyByPolicyIds(Long[] policyIds)
    {
        for (Long l: policyIds){
            FyPolicy fyPolicy=new FyPolicy();
            fyPolicy.setIsDelete(1l);
            fyPolicy.setPolicyId(l);
            fyPolicyMapper.updateFyPolicy(fyPolicy);
            EsSearch esSearch=new EsSearch();
            esSearch.setId(fyPolicy.getPolicyId());
            esSearch.setLanmu("政策");
            esSearch.setLanmuId(4);
            esUnService.delete(esSearch);
        }
        return 1;
    }

    /**
     * 删除政策信息
     *
     * @param policyId 政策主键
     * @return 结果
     */
    @Override
    public int deleteFyPolicyByPolicyId(Long policyId)
    {
        return fyPolicyMapper.deleteFyPolicyByPolicyId(policyId);
    }
}
